package com.ny.shiyan;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class shiyan6 {
        static class Employee {
            private String name;
            private int age;
            private double salary;

            public Employee(String name, int age, double salary) {
                this.name = name;
                this.age = age;
                this.salary = salary;
            }

            public String getName() { return name; }
            public int getAge() { return age; }
            public double getSalary() { return salary; }


            @Override
            public String toString() {
                return name + "," + age + "," + salary;
            }
        }

        public static void main(String[] args) {

            List<Employee> employees = new ArrayList<>();
            employees.add(new Employee("张三", 28, 15000.0));
            employees.add(new Employee("李四", 35, 25000.0));
            employees.add(new Employee("王五", 42, 32000.0));


            String filePath = "employees.txt";

            saveEmployeesToFile(employees, filePath);
            System.out.println("员工信息已保存到文件: " + filePath);

            System.out.println("\n从文件读取的员工信息:");
            List<Employee> employeesFromFile = readEmployeesFromFile(filePath);
            printEmployees(employeesFromFile);
        }

        private static void saveEmployeesToFile(List<Employee> employees, String filePath) {
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
                for (Employee emp : employees) {
                    writer.write(emp.toString());
                    writer.newLine();
                }
            } catch (IOException e) {
                System.err.println("写入文件时出错: " + e.getMessage());
            }
        }

        private static List<Employee> readEmployeesFromFile(String filePath) {
            List<Employee> employees = new ArrayList<>();

            try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    String[] parts = line.split(",");
                    if (parts.length == 3) {
                        String name = parts[0];
                        int age = Integer.parseInt(parts[1]);
                        double salary = Double.parseDouble(parts[2]);
                        employees.add(new Employee(name, age, salary));
                    }
                }
            } catch (IOException e) {
                System.err.println("读取文件时出错: " + e.getMessage());
            } catch (NumberFormatException e) {
                System.err.println("数据格式错误: " + e.getMessage());
            }

            return employees;
        }

        private static void printEmployees(List<Employee> employees) {
            System.out.println("| 姓名   | 年龄 | 薪资    |");
            for (Employee emp : employees) {
                System.out.printf("| %-6s | %-3d | %-7.2f |\n",
                        emp.getName(), emp.getAge(), emp.getSalary());
            }
        }
    }
